package cc.rengu.redp.bizimpl.upmp.controller;

import cc.rengu.redp.bizimpl.upmp.domain.BizTmp;
import cc.rengu.redp.bizimpl.upmp.service.IBizTmpService;
import cc.rengu.redp.common.annotation.Log;
import cc.rengu.redp.common.controller.BaseController;
import cc.rengu.redp.common.domain.QueryRequest;
import cc.rengu.redp.common.domain.RedpResponse;
import cc.rengu.redp.common.exception.RedpException;
import cc.rengu.redp.bizimpl.upmp.domain.CupsInstInfo;
import cc.rengu.redp.bizimpl.upmp.service.CupsInstInfoService;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import java.util.Map;

/**
 * @author LiuYuLiang.LL
 */
@Slf4j
@Validated
@Api(tags = "参数管理:银联机构号管理")
@RestController
@RequestMapping("/admin/merParam/cupsInstInfo")
public class CupsInstInfoController extends BaseController {
    @Autowired
    private CupsInstInfoService cupsInstInfoService;
    @Autowired
    private IBizTmpService bizTmpService;

    @GetMapping
//    @RequiresPermissions("cupsInstInfo:view")
    public Map<String, Object> list(QueryRequest request, CupsInstInfo entity) {
        return getDataTable(this.cupsInstInfoService.findByPage(request, entity));
    }

    @Log("查询单条")
    @GetMapping(value = "/getOne")
    @ApiImplicitParam(paramType = "query", name = "id", value = "id主键", required = true, dataType = "String")
    public CupsInstInfo getOne(@NotBlank(message = "{required}") String id) {
        return cupsInstInfoService.getById(id);
    }

    @Log("查询审核数据")
    @GetMapping(value = "/getCheckOne")
    @ApiImplicitParam(paramType = "query", name = "id", value = "id主键", required = true, dataType = "String")
    public CupsInstInfo getCheckOne(@NotBlank(message = "{required}") String id) throws RedpException {
        CupsInstInfo entity = cupsInstInfoService.getById(id);
        //获取临时表数据并转换成实体对象
        BizTmp tmp = bizTmpService.getBizTmpDetail(String.valueOf(id));
        if (tmp == null) {
            log.error("根据id没有找到指定临时表数据！");
            throw new RedpException("根据id没有找到指定临时表数据！");
        }
        String bizdata = "";
        if (null != tmp.getBizData1()) {
            bizdata += tmp.getBizData1();
        }
        if (null != tmp.getBizData2()) {
            bizdata += tmp.getBizData2();
        }
        if (null != tmp.getBizData3()) {
            bizdata += tmp.getBizData3();
        }
        entity = JSON.parseObject(bizdata, CupsInstInfo.class);
        return entity;
    }

    @Log("新增")
    @PostMapping
//    @RequiresPermissions("cupsInstInfo:add")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "cupsInstId", value = "银联机构号不能为空", required = true, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "cupsInstName", value = "银联机构名称不能为空", required = true, dataType = "String")
    })
    public void add(@Valid CupsInstInfo entity) throws RedpException {
        try {
            this.cupsInstInfoService.create(entity);
        } catch (Exception e) {
            String message = "新增失败";
            log.error(message, e);
            throw new RedpException(message);
        }
    }

    @Log("修改")
    @PutMapping("/update")
//    @RequiresPermissions("cupsInstInfo:update")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "id", value = "id主键", required = true, dataType = "String")
    })
    public void update(@Valid CupsInstInfo entity) throws RedpException {
        try {
            this.cupsInstInfoService.update(entity);
        } catch (Exception e) {
            String message = "修改失败";
            log.error(message, e);
            throw new RedpException(message);
        }
    }

    @Log("审核")
    @PutMapping("/audit")
//    @RequiresPermissions("cupsInstInfo:update")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "id", value = "id主键", required = true, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "approvalRefuseReason", value = "审批拒绝原因", required = true, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "opt", value = "审核结果", required = true, dataType = "String")
    })
    public void audit(@Valid CupsInstInfo entity, String opt, String approvalRefuseReason) throws RedpException {
        try {
            this.cupsInstInfoService.audit(entity, opt, approvalRefuseReason);
        } catch (Exception e) {
            String message = "修改失败";
            log.error(message, e);
            throw new RedpException(message);
        }
    }

    @Log("注销")
    @PutMapping("/cancel")
//    @RequiresPermissions("cupsInstInfo:cancel")
    @ApiImplicitParam(paramType = "query", name = "id", value = "id主键", required = true, dataType = "String")
    public void cancel(@NotBlank(message = "{required}") String id) throws RedpException {
        try {
            this.cupsInstInfoService.cancel(id);
        } catch (Exception e) {
            String message = "注销失败";
            log.error(message, e);
            throw new RedpException(message);
        }
    }

    @Log("删除")
    @DeleteMapping("/{ids}")
//    @RequiresPermissions("cupsInstInfo:delete")
    @ApiImplicitParam(paramType = "path", name = "ids", value = "id主键，逗号隔开", required = true, dataType = "String")
    public void delete(@NotBlank(message = "{required}") @PathVariable String ids) throws RedpException {
        try {
            this.cupsInstInfoService.delete(ids.split(StringPool.COMMA));
        } catch (Exception e) {
            String message = "删除类目失败";
            log.error(message, e);
            throw new RedpException(message);
        }
    }

    @PostMapping(value = "/check")

    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "id", value = "id主键", dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "cupsInstId", value = "银联机构号", dataType = "String")
    })
    public RedpResponse check(String id, String cupsInstId) throws RedpException {
        LambdaQueryWrapper<CupsInstInfo> wrapper = new LambdaQueryWrapper();
        if (StringUtils.checkValNotNull(cupsInstId)) {
            if (StringUtils.checkValNotNull(id)) {
                wrapper.ne(CupsInstInfo::getId, id);
            }
            wrapper.eq(CupsInstInfo::getCupsInstId, cupsInstId);
            CupsInstInfo info = cupsInstInfoService.getOne(wrapper);
            if (null != info) {
                throw new RedpException("[ 银联机构号 ] 重复，请重新输入");
            }
        }
        return new RedpResponse().message("验证成功");
    }

    @GetMapping(value = "/select2")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "q", value = "银联机构号|银联机构名称", dataType = "String"),
    })
    public Map<String, Object> select2(String q, QueryRequest request) {

        return cupsInstInfoService.select2(q, request);

    }
}
